<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin\AdminMenu;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Admin\CommonController;
use App\Repository\IndexRepository;

/**
 * 导航，面包屑,
 * 待添加缓存 判断是否更新进行缓存
 */
class IndexController extends Controller
{
    /**
     * 栏目管理页面渲染
     *
     * @return void
     */
    public function menu()
    {
        $data['tree'] = IndexRepository::tree(AdminMenu::get());
        return view('admin.menu.index',$data);
    }

    /**
     * 获取目录
     *
     * @return void
     * @author zhaoweichao
     */
    public function getMenuApi(Request $request)
    {
        $data = IndexRepository::lists($request->all());
        return response()->json($data);
    }

    /**
     * 静态数据渲染
     *
     * @return void
     */
    static public function getData()
    {
        $data = Cache::remember('MenuCacheData', 2, function (){
            return AdminMenu::get();
        });
        return $data->toArray();
    } 

    /**
     * Menu模块渲染
     */
    static public function getMenu()
    {
        $data =  self::getData();
        return IndexRepository::getMenu($data);
    }
    
    /**
     * 添加栏目信息处理
     *
     * @param Request $request
     * @return void
     */
    public function addMenu(Request $request)
    {
       return $this->admin_menu->create($request->all());
    }
    
    /**
     * 面包屑
     *
     * @return void
     */
    static public function getCrumbs()
    {
        $data = self::getData();
        return IndexRepository::getCrumbs($data);
    }
}
